﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace CodeCreator
{
    public class ToolsSerialize
    {
        /// <summary>
        /// 对象序列化成 XML String
        /// </summary>
        public static void XmlSerialize<T>(T obj, string filePath)
        {
            FileInfo fileInfo = new FileInfo(filePath);
            if (!Directory.Exists(fileInfo.DirectoryName))
                Directory.CreateDirectory(fileInfo.DirectoryName);

            //创建文件流
            Stream fStream = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite, FileShare.None);

            XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
            xmlSerializer.Serialize(fStream, obj);
            fStream.Flush();
            if (fStream != null)
            {
                fStream.Close();
            }

        }

        /// <summary>
        /// 
        /// </summary>
        public static void BitSerialize<T>(T obj, string filePath)
        {
            FileInfo fileInfo = new FileInfo(filePath);
            if (!Directory.Exists(fileInfo.DirectoryName))
                Directory.CreateDirectory(fileInfo.DirectoryName);

            //创建文件流
            Stream fStream = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite, FileShare.None);;

            //使用二进制序列化器
            BinaryFormatter binFormat = new BinaryFormatter();
            //将list序列化到文件中
            binFormat.Serialize(fStream, obj);
            fStream.Flush();
            if (fStream != null)
            {
                fStream.Close();
            }
        }

        /// <summary>
        /// XML String 反序列化成对象
        /// </summary>
        public static T XmlDeserialize<T>(string filePath)
        {
            T t = default(T);

            //读文件
            if (File.Exists(filePath))
            {
                StreamReader fStream = new StreamReader(filePath,Encoding.UTF8);
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
                Object obj = xmlSerializer.Deserialize(fStream);
                t = (T)obj;
                //关闭文件流
                if (fStream != null)
                {
                    fStream.Close();
                }
            }
            return t;
        }

      

        /// <summary>
        /// 二进制文件反序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static T BitDeserialize<T>(string filePath)
        {
            T t = default(T);
            //创建文件流
            if (File.Exists(filePath))
            {
                Stream fStream = File.Open(filePath, FileMode.Open);
                //使用二进制序列化器
                BinaryFormatter binFormat = new BinaryFormatter();
                t = (T)binFormat.Deserialize(fStream);
                //关闭文件流
                if (fStream != null)
                {
                    fStream.Close();
                }
            }
            return t;
        }

    }
}
